//
//  LGRichTextToolBar.m
//  Test
//
//  Created by 刘亚军 on 2022/6/16.
//  Copyright © 2022 刘亚军. All rights reserved.
//

#import "LGRichTextToolBar.h"
#import "LGKConst.h"

@interface LGRichTextToolBar ()
@property (nonatomic,strong) UIToolbar *toolBar;
@end
@implementation LGRichTextToolBar
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews{
    _toolBar = [[UIToolbar alloc] initWithFrame:self.bounds];
    _toolBar.barTintColor = [UIColor whiteColor];
    [self addItems];
    [self addSubview:_toolBar];
}
- (void)setHidePhotoTool:(BOOL)hidePhotoTool{
    _hidePhotoTool = hidePhotoTool;
    [self addItems];
}
- (void)addItems{
    UIBarButtonItem *navigationSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    navigationSpacer.width = 10;
    UIBarButtonItem *navigationMargin = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    navigationMargin.width = 30;
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *camera = [[UIBarButtonItem alloc]initWithImage:[[UIImage yj_imageNamed:@"note_camera" atBundle:LGKNoteManager.defaultManager.noteBundle] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(toolBarEvent:)];
    camera.tag = LGRichTextToolBarTypeCamera;
    
    UIBarButtonItem *photo = [[UIBarButtonItem alloc]initWithImage:[[UIImage yj_imageNamed:@"note_pho_unselected" atBundle:LGKNoteManager.defaultManager.noteBundle] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(toolBarEvent:)];
    photo.tag = LGRichTextToolBarTypePhoto;
    
    UIBarButtonItem *drawBoard = [[UIBarButtonItem alloc]initWithImage:[[UIImage yj_imageNamed:@"note_draw" atBundle:LGKNoteManager.defaultManager.noteBundle] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(toolBarEvent:)];
    drawBoard.tag = LGRichTextToolBarTypeDrawBoard;
    
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"收起" style:UIBarButtonItemStyleDone target:self action:@selector(toolBarEvent:)];
    done.tag = LGRichTextToolBarTypeDone;
    if (self.hidePhotoTool){
        [_toolBar setItems:@[space,done,navigationSpacer]];
    }else{
        [_toolBar setItems:@[space,camera,navigationMargin,photo,navigationMargin,drawBoard,navigationMargin,done,navigationSpacer]];
    }
}
- (void)toolBarEvent:(UIBarButtonItem *)sender{
    switch (sender.tag) {
        case LGRichTextToolBarTypeCamera:
            if (self.delegate && [self.delegate respondsToSelector:@selector(LGRichTextToolBarCamera)]) {
                [self.delegate LGRichTextToolBarCamera];
            }
            break;
        case LGRichTextToolBarTypePhoto:
            if (self.delegate && [self.delegate respondsToSelector:@selector(LGRichTextToolBarPhoto)]) {
                [self.delegate LGRichTextToolBarPhoto];
            }
            break;
        case LGRichTextToolBarTypeDrawBoard:
            if (self.delegate && [self.delegate respondsToSelector:@selector(LGRichTextToolBarDrawBoard)]) {
                [self.delegate LGRichTextToolBarDrawBoard];
            }
            break;
        case LGRichTextToolBarTypeUndo:
            if (self.delegate && [self.delegate respondsToSelector:@selector(LGRichTextToolBarUndo)]) {
                [self.delegate LGRichTextToolBarUndo];
            }
            break;
        case LGRichTextToolBarTypeDone:
            if (self.delegate && [self.delegate respondsToSelector:@selector(LGRichTextToolBarDone)]) {
                [self.delegate LGRichTextToolBarDone];
            }
            break;
        default:
            break;
    }
}
@end
